package org.serverct.finalfight.stat.gui;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.Inventory;
import org.serverct.finalfight.game.hadesgame.EventType;
import org.serverct.finalfight.stat.IPlayer;
import org.serverct.finalfight.stat.StatManager;
import org.serverct.finalfight.util.CustomSettings;

import java.util.HashSet;
import java.util.UUID;

public class InfoGuiListener implements Listener {
    public static HashSet<UUID> openers = new HashSet<>();

    @EventHandler
    public void onOpen(InventoryOpenEvent event) {
        if (event.getInventory().getHolder() instanceof InfoGui.InfoGuiHolder) {
            openers.add(event.getPlayer().getUniqueId());
        }
    }

    @EventHandler
    public void onClose(InventoryCloseEvent event) {
        if (event.getInventory().getHolder() instanceof InfoGui.InfoGuiHolder) {
            openers.remove(event.getPlayer().getUniqueId());
        }
    }

    @EventHandler
    public void onClickInv(InventoryClickEvent event) {
        Inventory inv = event.getClickedInventory();
        if (inv == null) {
            return;
        }
        Player player = (Player) event.getWhoClicked();
        if (openers.contains(player.getUniqueId())) {
            event.setCancelled(true);
            if (inv.getHolder() instanceof InfoGui.InfoGuiHolder) {
                if (event.getSlot() == 5) {
                    IPlayer stat = StatManager.stats.get(player.getUniqueId());
                    player.sendMessage(CustomSettings.getLanguageWithPrefix("experience.head"));
                    for (EventType type : stat.experience.keySet()) {
                        player.sendMessage(CustomSettings.getLanguageWithPrefix("experience.text")
                                .replace("%event%", type.name())
                                .replace("%time%", String.valueOf(stat.experience.get(type))));
                    }
                }
            }
        }
    }
}
